/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.comac.ins.isfd.domain.vo.IsfdDataAnalysisVo;
import com.comac.ins.isfd.mapper.IsfdDataAnalysisMapper;
import com.comac.ins.isfd.service.IIsfdDataAnalysisService;
import com.comac.ins.isfd.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author wujialin
 * @date 2024-11-07
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class IsfdDataAnalysisServiceImpl implements IIsfdDataAnalysisService {

    @Autowired
    private final IsfdDataAnalysisMapper baseMapper;

    @Override
    public List<IsfdDataAnalysisVo> queryLayupCount(Date startTime, Date endTime) {
        List<IsfdDataAnalysisVo> rawData = baseMapper.queryLayupCount(startTime, endTime);

        List<String> allMonths = DateUtils.getAllMonthsBetween(startTime, endTime);

        Set<String> tableTitles = Set.of("layup_equivalent_material", "layup_metal_case_meta", "layup_skin_propertie");

        List<IsfdDataAnalysisVo> result = new ArrayList<>();

        for (String month : allMonths) {
            for (String title : tableTitles) {
                // 查找是否有该月份和表的记录
                Long count = rawData.stream()
                    .filter(data -> month.equals(data.getTime()) && title.equals(data.getTitle()))
                    .map(IsfdDataAnalysisVo::getTotalCount)
                    .findFirst()
                    .orElse(0L);

                result.add(new IsfdDataAnalysisVo(month, title, count));
            }
        }

        result.sort(Comparator.comparing(IsfdDataAnalysisVo::getTime));

        return result;
    }
}
